package com.woniu.order.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.order.bean.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class RedisSeachUtil {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    
    public OverAll getRedis(Integer orderId) throws JsonProcessingException {
        ObjectMapper objectMapper=new ObjectMapper();

        OverAll overAll=new OverAll();
        String logisticsKey = "logistics:"+ orderId + ":save";
        String basiKey = "basi:"+ orderId + ":save";
        String goodsKey = "goods:"+ orderId + ":save";
        String costKey = "cost:"+ orderId + ":save";
        String addressKey = "address:"+ orderId + ":save";
        System.out.println("logisticsKey = " + logisticsKey);
        System.out.println("stringRedisTemplate = " + stringRedisTemplate);
        String logis = stringRedisTemplate.opsForValue().get(logisticsKey);
        
        String basi = stringRedisTemplate.opsForValue().get(basiKey);
        String goods = stringRedisTemplate.opsForValue().get(goodsKey);
        String cost = stringRedisTemplate.opsForValue().get(costKey);
        String addr = stringRedisTemplate.opsForValue().get(addressKey);
        if(StringUtils.hasText(basi)){
            TOrderbasicinfo tOrderbasicinfo = objectMapper.readValue(basi, TOrderbasicinfo.class);
            overAll.settOrderbasicinfo(tOrderbasicinfo);
        }
        if(StringUtils.hasText(goods)){
            TGoods tGoods = objectMapper.readValue(goods, TGoods.class);
            overAll.settGoods(tGoods);
        }
        if(StringUtils.hasText(cost)){
            TCost tCost = objectMapper.readValue(cost, TCost.class);
            overAll.settCost(tCost);
        }
        if(StringUtils.hasText(addr)){
            Address address = objectMapper.readValue(addr, Address.class);
            overAll.setAddress(address);
        }
        if(StringUtils.hasText(logis)){
            TLogistics logistics = objectMapper.readValue(logis, TLogistics.class);
            overAll.settLogistics(logistics);
        }
        
        return overAll;
    }
    
    
    
}
